<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        v-if="isP('GenSaveDbField')"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-plus"
        @click="saveDbField()"
      >保存
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        label="字段代码"
        width="140"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.fieldcode"
            size="small"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="字段名称"
        width="160"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.fieldname"
            size="small"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="字段类型"
        width="120"
      >
        <template slot-scope="scope">
          <el-select v-model="scope.row.type" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in fieldTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="字段长度"
        width="100"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.length"
            size="small"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="小数位数"
        width="100"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.point"
            size="small"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="输入长度"
        width="100"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.inputLength"
            size="small"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="是否为空"
        width="100"
      >
        <template slot-scope="scope">
          <el-select v-model="scope.row.isBlank" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in yesNoOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="是否列表字段"
        width="120"
      >
        <template slot-scope="scope">
          <el-select v-model="scope.row.isListField" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in yesNoOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="列表字段宽度"
        width="120"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.listWidth"
            size="small"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="列表字段对齐"
        width="120"
      >
        <template slot-scope="scope">
          <el-select v-model="scope.row.listAlign" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in listAlignOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="是否查询字段"
        width="120"
      >
        <template slot-scope="scope">
          <el-select v-model="scope.row.isSearchField" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in yesNoOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="排序"
        width="100"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.displayOrder"
            size="small"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { loadDbField, saveDbField } from '@/api/gen/create'
import msgUtil from '@/utils/msgUtil'
import PageUtil from '@/utils/PageUtil'
import ComboConst from '@/utils/ComboConst'
import routerUtil from '@/utils/routerUtil'

export default {
  data() {
    return {
      authorId: this.$route.query.authorId,
      tableId: this.$route.query.tableId,
      fieldTypeOptions: ComboConst.fieldTypeOptions,
      yesNoOptions: ComboConst.yesNoOptions,
      listAlignOptions: ComboConst.listAlignAllOptions,
      list: null,
      listLoading: false,
      listQuery: {
        tableId: this.$route.query.tableId.toString(),
        authorId: this.$route.query.authorId.toString()
      }
    }
  },
  created() {
    if (this.listQuery.tableIdId !== '') {
      this.loadDbField()
    }
  },
  methods: {
    fieldTypeFormatter(row) {
      return PageUtil.formatValue(ComboConst.fieldTypeOptions, row.type)
    },
    loadDbField() {
      if (this.listQuery.tableId === '') {
        msgUtil.error2('请选择数据表')
        return
      }
      this.listLoading = true
      loadDbField(this.listQuery).then(response => {
        this.list = response.data
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
    },
    // 保存数据库字段
    saveDbField() {
      // 获取所有字段，拼成一个对象
      console.log(this.list)
      msgUtil.confirm('确认要覆盖既有的字段配置吗？').then(async() => {
        await
        saveDbField(this.list).then(response => {
          msgUtil.success()
          routerUtil.backPath(this, '/gen/table/index')
        })
      })
    }
  }
}
</script>
